package com.ziq.zcamp.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;

import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.simple.SimpleComponent;
import com.ziq.zcamp.R;

@SuppressWarnings({"unused", "UnusedReturnValue"})
public class CarRefreshHeader extends SimpleComponent implements RefreshHeader {

    public LoadingView loadingView;

    public CarRefreshHeader(Context context) {
        this(context, null);
    }

    public CarRefreshHeader(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        View.inflate(context, R.layout.view_car_refresh_header, this);
        loadingView = findViewById(R.id.loading_view);
    }

    public interface UpdateLoadingViewCallback{
        void onUpdate(LoadingView loadingView);
    }

    public void updateLoadingView(UpdateLoadingViewCallback callback){
        callback.onUpdate(loadingView);
    }

    public interface LoadingStateChangeListener{
        void onStateChanged(@NonNull RefreshState oldState, @NonNull RefreshState newState);
    }

    public LoadingStateChangeListener mLoadingStateChangeListener;
    public void setLoadingStateChangeListener(LoadingStateChangeListener callback){
        mLoadingStateChangeListener = callback;
    }


    @Override
    public int onFinish(@NonNull RefreshLayout layout, boolean success) {
        loadingView.stopAnim();
        return super.onFinish(layout, success);//延迟500毫秒之后再弹回
    }



    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        if(mLoadingStateChangeListener != null){
            mLoadingStateChangeListener.onStateChanged(oldState, newState);
        }
        switch (newState) {
            case None:
            case PullDownToRefresh:
                break;
            case Refreshing:
                loadingView.startAnim();
                break;
            case RefreshReleased:
                break;
            case ReleaseToRefresh:
                break;
            case ReleaseToTwoLevel:
                break;
            case Loading:
                break;
        }
    }
}
